<style scoped>

</style>
<template>
    <div class="page" v-loading="loading">
        <el-row class="m-b-0">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                    <el-button type="primary" size="medium" icon="el-icon-plus"
                               @click="$router.push('/articles/edit')">添加文章
                    </el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="danger" size="medium" icon="el-icon-delete" @click="deleteArticles">删除选中文章
                    </el-button>
                </el-form-item>
                <el-form-item label="">
                    <el-cascader style="width: 100%"
                                 placeholder="按分类筛选"
                                 @change="()=>{filter.page=1; getData()}"
                                 v-model="filter.categoryId"
                                 :options="categories"
                                 :props="{ checkStrictly: true,label:'name',value:'id', emitPath:false, expandTrigger:'hover' }"
                                 clearable></el-cascader>
                </el-form-item>
                <el-form-item label="">
                    <el-input placeholder="按关键字搜索" v-model="filter.keywords" clearable>
                        <el-button slot="append" icon="el-icon-search" @click="()=>{ filter.page=1; getData() }"></el-button>
                    </el-input>
                </el-form-item>
            </el-form>

        </el-row>
        <el-table
                ref="articleTable"
                :data="tableData.items"
                tooltip-effect="dark"
                style="width: 100%"
                @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50"></el-table-column>
            <el-table-column prop="id" label="Id" width="80" show-overflow-tooltip></el-table-column>
            <el-table-column prop="title" label="标题">
                <template slot-scope="scope">
                    <!--<a :href="'/content/'+scope.row.id" target="_blank">{{scope.row.title}}</a>-->
                    {{scope.row.title}}
                </template>
            </el-table-column>
            <el-table-column prop="viewCount" label="访问量" width="120"></el-table-column>
            <el-table-column label="属性" width="160">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.recommend" style="margin-right: 5px">荐</el-tag>
                    <el-tag type="info" v-if="scope.row.onlySpider" style="margin-right: 5px">蜘蛛</el-tag>
                    <el-tag type="primary" v-if="scope.row.focus">焦</el-tag>
                </template>
            </el-table-column>
            <!--<el-table-column prop="category" abel="分类"></el-table-column>-->
            <el-table-column prop="creationTime" label="创建时间" width="200" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button @click="$router.push({path:'/articles/edit',query:{id:scope.row.id}})" type=""
                               size="small">编辑
                    </el-button>
                    <el-button type="danger" size="small" @click="handleDelete([scope.row.id])" plain>删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="block m-t-20">
            <el-pagination
                    @size-change="(_size)=>{ filter.size = _size; getData()}"
                    @current-change="(p)=>{ filter.page = p; getData()}"
                    :current-page="filter.page"
                    :page-sizes="defaultPageSize"
                    :page-size="filter.size"
                    layout="total, sizes, prev, pager, next, jumper"
                    background
                    :total="tableData.totalCount">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import Axios from 'axios'

    let pagerHeader = {
        title: '文章管理',
        desc: '在这里可以搜索、修改、删除文章。',
        breadcrumb: ['系统', '文章管理']
    }
    export default {
        name: 'articleList',
        data() {
            return {
                categories: [],
                loading: false,
                filter: {
                    keywords: null,
                    categoryId: null,
                    page: 1,
                    size: 20,
                },
                tableData: {
                    items: [],
                    totalCount: 0
                },
                multipleSelection: []
            }
        },
        activated() {
            this.init()
        },
        created() {
            this.init();
        },
        methods: {
            init() {
                this.setPageHead(pagerHeader);
                //加载分类和文章
                this.getCategories();
                this.getData();
            },
            getCategories() {
                this.$apis.articles.getCategoryTreeList({page:1,size:15}).then(r => {
                    console.log(r)

                    this.categories = r.result;
                }).catch(e => {
                })
            },
            getData() {
                this.loading = true;
                this.$apis.articles.getAll(this.filter).then(r => {
                    this.loading = false;
                    if (r.success) {
                        this.tableData = r.result
                    } else {
                        this.$msg.alert.error(r.error.message)
                    }
                }).catch(e => {
                    this.loading = false;
                })
            },


            deleteArticles() {
                if (this.multipleSelection.length === 0) {
                    this.$message.warning('请选中要删除的数据')
                    return
                }
                this.handleDelete(this.multipleSelection.map(x => x.id))
            },
            handleDelete(ids) {
                this.$confirm('此操作将永久删除数据, 是否继续?', '', {
                    confirmButtonText: '是',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.loading = true;
                    this.$apis.articles.deletes(ids).then(r => {
                        this.loading = false;
                        if (r.success) {
                            this.getData()
                            this.$msg.notify.success('删除成功');
                        } else {
                            this.$msg.alert.error(r.error.message)
                        }
                    }).catch(e => {
                        this.loading = false;
                    })
                }).catch(() => {
                })
            },
            handleSelectionChange(val) {
                this.multipleSelection = val
            },
        }
    }
</script>
